package com.ws.controller;

import com.ws.bean.Carousel;
import com.ws.bean.Routes;
import com.ws.service.CarouselService;
import com.ws.service.RouteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/carousel")
public class CarouselController {
    @Autowired
    RouteService routeService;

    @Autowired
    private CarouselService carouselService;

    @RequestMapping("add")
    public String add(Integer route_id){
        Routes route = routeService.getRouteById(route_id);
        //写一个修改方法把id为route_id的线路的is_banner设置为1
        route.setIs_banner(1);
        routeService.update(route);
        Carousel carousel = new Carousel();
        carousel.setImageURL(route.getRoute_image());
        carousel.setRoute_id(route_id);
        carousel.setCarousel_order(0);
        carouselService.add(carousel);
        return "redirect:list";
    }
    // 跳转到轮播图列表
    @RequestMapping("list")
    public String list(Model model){
        List<Carousel> list = carouselService.list();
        model.addAttribute("list",list);
        return "/carousel/list";
    }
    // 删除轮播图
    @RequestMapping("delete")
    public String delete(Integer carousel_id,Integer route_id){

        System.out.println(carousel_id+"-----------------"+route_id);

        Routes route = routeService.getRouteById(route_id);
        route.setIs_banner(0);
        routeService.update(route);
        carouselService.delete(carousel_id);
        return "redirect:list";
    }
    // 跳转到修改轮播图
    @RequestMapping("toUpdate")
    public String toUpdate(Integer carousel_id,Model model){
        Carousel carousel = carouselService.getCarouselById(carousel_id);
        model.addAttribute("carousel",carousel);
        return "/carousel/update";
    }
    // 修改轮播图
    @RequestMapping("update")
    public String update(Carousel carousel){
        carouselService.update(carousel);
        return "redirect:list";
    }
}
